summaryrefslogtreecommitdiffstats
path: root/src/hid_core/resources/irs_ring_lifo.h
blob: 255d1d296834085601ae13780c059d73b7fd9fd6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// SPDX-FileCopyrightText: Copyright 2022 yuzu Emulator Project
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <array>

#include "common/common_types.h"

namespace Service::IRS {

template <typename State, std::size_t max_buffer_size>
struct Lifo {
    s64 sampling_number{};
    s64 buffer_count{};
    std::array<State, max_buffer_size> entries{};

    const State& ReadCurrentEntry() const {
        return entries[GetBufferTail()];
    }

    const State& ReadPreviousEntry() const {
        return entries[GetPreviousEntryIndex()];
    }

    s64 GetBufferTail() const {
        return sampling_number % max_buffer_size;
    }

    std::size_t GetPreviousEntryIndex() const {
        return static_cast<size_t>((GetBufferTail() + max_buffer_size - 1) % max_buffer_size);
    }

    std::size_t GetNextEntryIndex() const {
        return static_cast<size_t>((GetBufferTail() + 1) % max_buffer_size);
    }

    void WriteNextEntry(const State& new_state) {
        if (buffer_count < static_cast<s64>(max_buffer_size)) {
            buffer_count++;
        }
        sampling_number++;
        entries[GetBufferTail()] = new_state;
    }
};

} // namespace Service::IRS